package com.yiren.dbaa.module.practice_set.site.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.contract.ReleaseContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/10/26 13:53
 * @description:
 */
public class ReleaseModel implements ReleaseContract.Model {

    private Activity activity;
    private Gson gson;
    private UserParamConfigBean userParamConfigBean;

    public ReleaseModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void publishRule(SiteEntity site, String userId, String updateDetails, int updateMode,CommonCallBack callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("siteCode", site.getSiteCode());
        map.put("userId", userId);
        map.put("ruleVer", site.getRuleVerLast()+1);
        map.put("baseVer", site.getBaseVer());
        map.put("useRange", site.getUseRange());
        map.put("updateDetails", updateDetails);
        map.put("updateMode", updateMode);
        map.put("publishTime", DateUtil.getCurrentDate());
        map.put("state", 1);
        if (userParamConfigBean.isPartner()){
            if (site.getPartneModifyVal() == null)
                site.setPartneModifyVal("");
            map.put("modifyVal",site.getPartneModifyVal());
        }else {
            if (site.getCoachModifyVal() == null)
                site.setCoachModifyVal("");
            map.put("modifyVal",site.getCoachModifyVal());
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                String line = OkHttpUtils.getInstance().z(AddressConstant.publishRule().getUrl(),new Gson().toJson(map));
                TestBaseBean<String> testBaseBean = gson.fromJson(line,new TypeToken<TestBaseBean<String>>() {}.getType());
                if (testBaseBean.isSuccess()){
                    callBack.success(testBaseBean.getData());
                }else {
                    callBack.fail("设置失败!");
                }
            }
        }).start();

       /* OkHttpUtils.getInstance().postJsonString(activity,AddressConstant.publishRule(), new Gson().toJson(map), new StringCallback() {
            @Override
            public void getString(String result) {
                callBack.success(result);
            }

            @Override
            public void getFail() {
                callBack.fail("");
            }
        });*/
    }
}
